package com.hscm.boss.mvc.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hscm.boss.PowerBossApplication;
import com.hscm.boss.common.enums.PublicStatus;
import com.hscm.boss.common.enums.ResponseCode;
import com.hscm.boss.common.exception.BossException;
import com.hscm.boss.common.utils.NoUtil;
import com.hscm.boss.dao.entity.PayWay;
import com.hscm.boss.dao.entity.UserInfo;
import com.hscm.boss.dao.mapper.PayWayMapper;
import com.hscm.boss.dao.result.DicEntity;
import com.hscm.boss.mvc.model.from.payWay.ConfigProductWayFrom;
import com.hscm.boss.mvc.service.PayWayService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class PayWayServiceImpl implements PayWayService {

    @Resource
    private PayWayMapper payWayMapper;


    @Override
    public IPage<PayWay> pageList(Integer pageNo, Integer pageSize, String productCode) {
        QueryWrapper<PayWay> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("pay_product_code", "way_code", "type_code");


        if (StrUtil.isNotBlank(productCode)) {
            wrapper.eq("pay_product_code", productCode.trim());
        }

        return payWayMapper.selectPage(Page.of(pageNo, pageSize), wrapper);
    }

    @Override
    public List<DicEntity> getPayWayListFromCache() {
        return PowerBossApplication.PAYWAY_DIC_LIST;
    }

    @Override
    public List<DicEntity> getPayTypeDicListFromCache(String payWayCode) {
        List<DicEntity> dicEntityList = PowerBossApplication.TYPE_DIC_MAP.get(payWayCode);
        return dicEntityList == null ? new ArrayList<>() : dicEntityList;
    }

    @Override
    public void configProductPayWay(ConfigProductWayFrom configProductWayFrom) {
        QueryWrapper<PayWay> wrapper = new QueryWrapper<>();
        wrapper.eq("pay_product_code", configProductWayFrom.getProductCode())
                .eq("way_code", configProductWayFrom.getWayCode())
                .eq("type_code", configProductWayFrom.getTypeCode());
        PayWay payWay = payWayMapper.selectOne(wrapper);

        if (payWay != null) {
            throw new BossException(ResponseCode.PAYMENT_CONFIG_EXISTS);
        }


        //添加支付方式的配置
        String wayCode = configProductWayFrom.getWayCode();
        String typeCode = configProductWayFrom.getTypeCode();

        payWay = new PayWay();
        payWay.setId(NoUtil.getUUID());
        payWay.setWayCode(wayCode);
        payWay.setWayName(PowerBossApplication.PAYWAY_DIC_MAP.get(wayCode));
        payWay.setTypeCode(typeCode);
        payWay.setTypeName(PowerBossApplication.TYPE_NAME_MAP.get(wayCode +":" + typeCode));
        payWay.setPayProductCode(configProductWayFrom.getProductCode());
        payWay.setPayRate(configProductWayFrom.getPayRate());
        payWay.setSorts(configProductWayFrom.getSorts());
        payWay.setStatus(PublicStatus.ENABLE.name());
        payWay.setEditTime(new Date());
        payWay.setRemark("配置支付产品的方式");

        payWayMapper.insert(payWay);

    }

    @Override
    public void removeByPayWayPrimary(String payWayPrimary) {
        payWayMapper.deleteById(payWayPrimary);

    }
}
